package com.wfh.easychat.controller;

import com.wfh.easychat.annotation.UserCheckLogin;
import com.wfh.easychat.common.BaseResponse;
import com.wfh.easychat.common.ErrorCode;
import com.wfh.easychat.common.ResultUtils;
import com.wfh.easychat.exception.BusinessException;
import com.wfh.easychat.model.entity.UserInfo;
import com.wfh.easychat.model.request.UserContactAddRequest;
import com.wfh.easychat.model.vo.UserContactVo;
import com.wfh.easychat.service.GroupInfoService;
import com.wfh.easychat.service.UserContactService;
import com.wfh.easychat.service.UserInfoService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;

/**
 * @Title: UserContactController
 * @Author wangfenghuan
 * @Package com.wfh.easychat.controller
 * @Date 2025/10/3 16:25
 * @description:
 */
@Slf4j
@RestController
@RequestMapping("/user/contact")
public class UserContactController {

    @Resource
    private UserContactService userContactService;

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private GroupInfoService groupInfoService;

    /**
     * 用户搜索
     * @param request
     * @param contactId
     * @return
     */
    @GetMapping("/search")
    @UserCheckLogin
    public BaseResponse<UserContactVo> search(HttpServletRequest request, Long contactId, Integer type){
        UserInfo loginUser = userInfoService.getLoginUser(request);
        Long id = loginUser.getId();
        UserContactVo userContactVo = userContactService.search(id, contactId, type);
        return ResultUtils.success(userContactVo);
    }

    /**
     * 申请添加好友
     *
     * @param request
     * @param userContactAddRequest
     * @return
     */
    @PostMapping("/applyAdd")
    @UserCheckLogin
    public BaseResponse<Integer> addUserContact(HttpServletRequest request, @RequestBody UserContactAddRequest userContactAddRequest) {
        UserInfo loginUser = userInfoService.getLoginUser(request);
        Long contactId = userContactAddRequest.getContactId();
        String applyInfo = userContactAddRequest.getApplyInfo();
        Integer type = userContactAddRequest.getType();
        if (contactId == null || type == null | applyInfo == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "id不能为空");
        }
        Integer joinType = userContactService.applyAdd(loginUser, contactId, applyInfo, type);
        return ResultUtils.success(joinType);
    }

}
